
import SDKAd from "./SDKAd";
import { SDKState, SDKDir, ResultCallback } from "../SDKDefine";
export default class SDKBannerAd extends SDKAd {


    //节点坐上角的坐标
    private _x: number = 0;
    private _y: number = 1;
    //节点宽高
    private _bannerWidth: number = 500
    private _bannerHeight: number = 300;

    protected _dir: number = SDKDir.BOTTOM_MID;
    protected _state: number = 0;
    constructor() {
        super();
        if (this.has()) {
            this.setDirectShow(false)
            this.load()
        }
    }

    open(param?: { callback: ResultCallback, dir: number }) {
        this._dir = param.dir;
        console.log('SGBannerAd open dir  ', this._dir, this._state)
        if (this._state == SDKState.SUCCESS) {
            this.updateStyle();
            this.show();
        } else {
            this.setDirectShow(true)
            this.load()
            this.instance.show();
        }
    }

    close() {
        this.setDirectShow(false)
        this.hide();
    }


    setPosition(x: number, y: number, width: number, height: number) {
        this._x = x;
        this._y = y;
        this._bannerWidth = width;
        this._bannerHeight = height;
    }

    show() {
        if (this.instance) {
            this.instance.show();
        }
    }

    hide() {
        if (this.instance) {
            this.instance.hide();
        }
    }


    private getStyle() {
        let winSize = this.sdk.getSystemInfoSync();
        console.log('SGBannerAd winSize  ', winSize, this._dir)
        let x = this._x;
        let y = this._y;
        switch (this._dir) {
            case SDKDir.BOTTOM_MID:
                x = (winSize.windowWidth - this._bannerWidth) / 2
                y = winSize.windowHeight - this._bannerHeight;
                break;
            case SDKDir.UP_MID:
                x = (winSize.windowWidth - this._bannerWidth) / 2
                y = 0
                break;
        }
        return {
            left: x,
            top: y,
            width: this._bannerWidth
        }
    }

    load() {
        console.log(' SDBannerAd load ')
        this._state = SDKState.LOADING;
        this.destroy();
        if (!this.instance) {
            this.instance = this.sdk.createBannerAd({
                adUnitId: this._adUnitId,
                posId: this._adUnitId,
                adIntervals: 30,
                style: this.getStyle()
            })
            this.instance.onLoad(this.bind(this.onLoad))
            this.instance.onError(this.bind(this.onError))
            this.instance.onResize(this.bind(this.onResize))
        }
        this.instance.load();


    }


    protected onResize(data: any) {
        this._bannerWidth = data.width;
        this._bannerHeight = data.height;
        this.updateStyle()
    }

    private updateStyle() {
        this.instance.style = this.getStyle();
    }

    private onLoad() {
        this._state = SDKState.SUCCESS;
        if (this._isDirectShow) {
            this.show();
            this._isDirectShow = false;
        }
    }

    private onError(err: any) {
        this._state = SDKState.FAIL
    }

    private destroy(): void {
        if (this.instance) {
            this.instance.offLoad(this.unbind(this.onLoad))
            this.instance.offError(this.unbind(this.onError))
            this.instance.offResize(this.unbind(this.onResize))
            this.instance.destroy();
            this.instance = null;
        }
    }
}
